<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('采购申请头文件')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
    <!-- <style type="text/css">.table {table-layout:fixed;}</style> -->
</head>

<body class="gray-bg">

<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="proPurchRequestHeaderForm" th:action="@{/sampleProRequest/list}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3" style="width: 33%">
                            <label>部门：</label>
                            <select id="purchRequestDept" name="purchRequestDept" sysDict="BM" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>成本中心：</label>
                            <select id="costCenter" name="costCenter" sysDict="CBZX" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>采购申请单号：</label>
                            <input type="text" name="purchRequestCode"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <div>
                                <label>申请者：</label>
                                <input type="text" name="purchRequestStaff"/>
                            </div>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>需求日期(起)：</label>
                            <input type="text" name="requestDate" id="requestDate"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>需求日期(止)：</label>
                            <input type="text" name="deliveryDate" id="deliveryDate"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>请购单状态：</label>
                            <select id="costCenter" name="goodsStatus" sysDict="HYZT" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <button id="proPurchRequestHeaderSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm"><i class="fa fa-search"></i>&nbsp;搜索</button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置</button>
                            <button id="proPurchRequestHeaderAdvancedSearch" type="button" class="btn btn-primary btn-rounded btn-sm" style="display: none"><i class="fa fa-caret-down"></i>&nbsp;高级搜索</button>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-info order-table">
            <div class="btn-group hidden-xs toolbar" id="proPurchRequestHeaderToolBar" role="group">
                <!-- <a class="btn btn-outline btn-success btn-rounded" onclick="toAddnproPurchRequestHeader()">
                    <i class="fa fa-plus"></i> 新增
                </a> -->
                <a class="btn btn-outline btn-success btn-rounded" onclick="inquiry()">
                    <i class="fa fa-save"></i> 询价
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="generateQuotations()">
                    <i class="fa fa-save"></i> 生成报价单
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="offer()">
                    <i class="fa fa-save"></i> 报价邮件通知
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="parity()">
                    <i class="fa fa-save"></i> 比价
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="supplyGood()">
                    <i class="fa fa-save"></i> 货源分配
                </a>
                <!-- <a class="btn btn-outline btn-success btn-rounded" onclick="toEditnproPurchRequestHeader()">
                    <i class="fa fa-edit"></i> 编辑
                </a> -->
            </div>
            <div style="height:360px; overflow:auto;">
                <table id="proPurchRequestHeader" data-mobile-responsive="true"></table>
            </div>
            <div>
                <table id="proPurchRequestItem" data-mobile-responsive="true"></table>
            </div>
        </div>
    </div>
</div>
<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<!-- Page-Level Scripts -->

<!--  -->
<script type="text/javascript">
    var itemBS = Searcher.createBS('#proPurchRequestItem',
        [
            {checkbox: true},
            {title: "采购申请编码", field:"purchRequestCode"},
            {title: "采购申请行项目编码",field:"purchRequestItemCode"},
            {title: "货源状态",field:"goodsStatus",
                formatter : function (value, row, index) {
                    if(value ==-2) {
                        return "草稿";
                    }else if(value == -1) {
                        return "采购申请询价中";
                    }else if(value == 0) {
                        return "货源已指定";
                    } else if(value == 1) {
                        return "订单已提交";
                    }else if(value == 2) {
                        return "订单审批中";
                    }else if(value == 3) {
                        return "订单审批通过";
                    }else if(value == -3) {
                        return "订单审批驳回";
                    }else {
                        return value;
                    }
                }
            },
            {title: "科目类别",field:"subjectCategory"},
            {title: "采购组",field:"purchGroup"},
            {title: "采购组织",field:"purchOrg"},
            {title: "使用人",field:"applier"},
            {title: "物料号",field:"materialCode"},
            {title: "物料描述",field:"materialDesc"},
            {title: "工厂",field:"factory"},
            {title: "库存地点",field:"storageLocation"},
            {title: "物料组",field:"materialGroup"},
            {title: "申请数量",field:"applyQty"},
            {title: "计量单位",field:"measureUnit"},
            {title: "成本中心",field:"costCenter"},
            {title: "内部订单号",field:"internalOrderCode"},
            {title: "主资产号",field:"mainAssetsCode"},
            {title: "资产次级编号",field:"assetsSencondCode"},
            {title: "需求日期",field:"requestDate"},
            {title: "交货日期",field:"deliveryDate"},
            {title: "交货地址",field:"deliveryAddress"},
            {title: "备注",field:"remark"}
        ], {"showColumns": false,"showRefresh": false, "firstLoad": false}
    );

    //面向对象
    var searcherBS= Searcher.createBS('#proPurchRequestHeader',
        [
            {radio: true},
            {title: "采购申请编码", field:"purchRequestCode"},
            {title: "采购申请类型 ",field:"purchRequestType"},
            {title: "创建人",field:"CreatedBy"},
            {title: "申请部门",field:"purchRequestDept"},
            {title: "流程状态",field:"goodsStatus",
                formatter : function (value, row, index) {
                    if(value ==-2) {
                        return "草稿";
                    } else if(value == -1) {
                        return "采购申请询价中";
                    }else if(value == 0) {
                        return "货源已指定";
                    } else if(value == 1) {
                        return "订单已提交";
                    }else if(value == 2) {
                        return "订单审批中";
                    }else if(value == 3) {
                        return "订单审批通过";
                    }else if(value == -3) {
                        return "订单审批驳回";
                    }else {
                        return value;
                    }
                }
            }
        ], {pageList: [5, 10, 15, 50, 100], pageSize : 5,
            onCheck : function(row) {itemBS.getData({'url': ctxPath + '/proPurchRequestItem/list?purchRequestCode='+row.purchRequestCode});},
            onPostBody : function() {$("#proPurchRequestItem").bootstrapTable('removeAll');}
        }
    );
    //刷新数据
    function refresh() {
        searcherBS.refresh();
        itemBS.refresh();
    }

    // 初始化时间插件
    laydate.render({elem: '#requestDate'});
    laydate.render({elem: '#deliveryDate'});

    initDictDataSel();//初始化所有字典数据
    /**
     * 跳转至新增页
     */
    function toAddnproPurchRequestHeader() {
        var index=Dialog.openUrl("新增采购订单", ctxPath + '/modules/requisition/requisition_add.html',{area : ['900px','700px'],
            maxmin: true,
            end: function() {}
        });
        layer.full(index);
    }

    /**
     * 编辑
     */
    function toEditnproPurchRequestHeader() {
        //field:"pkId"
        var rowDatas = searcherBS.getSelectedRows("pkId");
        if (rowDatas.length != 1) {
            Dialog.errorMsg("请选择一行进行编辑");
            return;
        }
        var index = Dialog.openUrl("编辑采购订单信息", ctxPath + '/proPurchRequestHeader/toEdit/'+rowDatas,
            {area : ['900px','700px'],
                maxmin: true,
                end: function () {}
            });
        layer.full(index)
    }

    function deletePurchRequestHeader() {
        var rowDatas = searcherBS.getSelectedRows("pkId");
        if (rowDatas.length < 1) {
            Dialog.errorMsg("请至少选择一行");
            return;
        }
        Dialog.confirm('确定删除？', function(index){
            Page.ajaxPOST(ctxPath + "/proPurchRequestHeader/delete", {userIds: rowDatas}, function () {
                Dialog.msg("操作成功", function () {
                    searcherBS.refresh();
                });
            });
        });
    }
    /**** 询价********/
    function inquiry(){
        //针对抬头来进行询价
        var rowData = searcherBS.getSelectedRows("pkId");
        var rowDatas = $("#proPurchRequestHeader").bootstrapTable('getSelections');
        //行项目的id
        var itemrowData = itemBS.getSelectedRows("pkId");
        var itemrowDatas = $("#proPurchRequestItem").bootstrapTable('getSelections');

        var goodsStatus;
        for(var i=0;i<itemrowDatas.length;i++){
            goodsStatus = itemrowDatas[i].goodsStatus;
        }
        if (rowData.length < 1 && itemrowData.length < 1) {
            Dialog.errorMsg("请至少选择一行进行编辑");
            return;
        }
        if(goodsStatus==-2){
            //2019-06-21 yanfei
            var index = Dialog.openUrl("新增询价单", ctxPath + '/proPurchRequestHeader/inquiry/'+rowData+'/'+itemrowData,
                {area : ['900px','700px'],
                    maxmin: true,
                    end: function () {}
                });
            layer.full(index)
        }
        if(goodsStatus==-1){
            Dialog.errorMsg("此采购订单已询价！！！");
        }
        if(goodsStatus==0){
            Dialog.errorMsg("此采购订单已货源分配！！！");
        }
        if(goodsStatus == 1) {
            Dialog.errorMsg("订单已提交！！！");
        }
        if(goodsStatus == 2) {
            Dialog.errorMsg("订单审批中！！！");
        }
        if(goodsStatus == 3) {
            Dialog.errorMsg("订单审批通过！！！");
        }
        if(goodsStatus == -3) {
            Dialog.errorMsg("订单审批驳回！！！");
        }
    }
    //生成报价单
    function generateQuotations(){
        var index = Dialog.openUrl("生成报价单", ctxPath + '/proPurchRequestHeader/generateQuotations/',
            {area : ['900px','700px'],
                maxmin: true,
                end: function () {}
            });
        layer.full(index)
    }
    /********报价*************/
    function offer(){
        var index = Dialog.openUrl("报价", ctxPath + '/proPurchRequestHeader/offer/',
            {area : ['900px','700px'],
                maxmin: true,
                end: function () {}
            });
        layer.full(index)
    }
    /***********比价****************/
    function parity(){
        var index = Dialog.openUrl("比价", ctxPath + '/proPurchRequestHeader/parity/',
            {area : ['900px','700px'],
                maxmin: true,
                end: function () {}
            });
        layer.full(index)
    }
    /************货源分配******************/
    function supplyGood(){
        //塞条件
        var rows = $("#proPurchRequestItem").bootstrapTable('getSelections');
        if(rows.length==1){
            for (var i=0;i<rows.length;i++){
                var  factory = rows[i].factory;
                var materialCode = rows[i].materialCode;
                $("#factory").val(factory);
                $("#materialCode").val(materialCode);
            }
            //check_form("essentialInformation");
            check_form("proPurchRequestItem");
        }
        if(rows.length==0||rows.length>1){
            Dialog.alert("请先选中一行！")
        }
    }
    function check_form(obj){
        //使用getSelections即可获得，row是json格式的数据
        var rows = $("#"+obj).bootstrapTable('getSelections');
        var rowDatas=new Array();
        //判断是否选中行
        if (!rows>0 || rows.length == 0) {
            Dialog.alert("请先选中一行！")
            return;
        }
        //遍历选中的值
        for (var i=0;i<rows.length;i++){
            //if(rows[i].goodsStatus==-1){
            if(rows[i].goodsStatus==-2||rows[i].goodsStatus==3){
                //后续加上pkid
                rowDatas.push(rows[i].materialCode+";"+rows[i].werks+";"+rows[i].srmCode);
                toEditPurchRequestItem(rowDatas);
            }else{
                /*if(rows[i].goodsStatus==-2){
                    return Dialog.alert("请先批量提交");
                }*/
                if(rows[i].goodsStatus==0){
                    return Dialog.alert("货源已经指定");
                }
                if(rows[i].goodsStatus==-1){
                    return Dialog.alert("货源正在询价中不允许分配货源");
                }
                if(rows[i].goodsStatus==2){
                    return Dialog.alert("价格审批中不允许分配货源");
                }
                if(rows[i].goodsStatus==-3){
                    return Dialog.alert("价格审批被驳回不允许分配货源");
                }
            }
        }
    }
    /**
     * 编辑多条信息
     */
    function toEditPurchRequestItem(rowDatas1) {
        var rowDatas = itemBS.getSelectedRows("pkId");
        if (rowDatas.length < 1) {
            Dialog.errorMsg("请至少选择一行进行编辑");
            return;
        }
        // itemBS.getData({'url': ctxPath + '/nproPurchRequestItem/list?purchRequestCode='+row.purchRequestCode});
        var index = Dialog.openUrl("货源分配信息", ctxPath + '/proPurchRequestItem/toAllot/?ids='+rowDatas+'&inqids='+rowDatas1,
            {area : ['900px','700px'],
                maxmin: true,
                end: function () {
                }
            });
        layer.full(index);
    }
</script>
</body>
</html>